package stack;


import java.util.Stack;

/**
 * @author Jonny Long
 * @date 2021/2/4 18:48
 */
public class Solution1544 {
    public String makeGood(String s) {
        //StringBuffer ret = new StringBuffer();
        //int retIndex = -1;
        //int len = s.length();
        //for (int i = 0; i < len; i++) {
        //    char ch = s.charAt(i);
        //    if (ret.length() > 0 &&
        //            Character.toLowerCase(ret.charAt(retIndex)) == Character.toLowerCase(ch) &&
        //            ret.charAt(retIndex) != ch) {
        //        ret.deleteCharAt(retIndex);
        //        retIndex--;
        //    } else {
        //        ret.append(ch);
        //        retIndex++;
        //    }
        //}
        //return ret.toString();
        Stack<Character> stack = new Stack<Character>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!stack.empty() &&
                    Character.toLowerCase(stack.peek()) == Character.toLowerCase(ch) &&
                    stack.peek()!=ch
            ) {
                stack.pop();
            }else {
                stack.push(ch);
            }
        }
        Stack<Character> stack2 = new Stack<Character>();
        while (!stack.empty()){
            stack2.push(stack.pop());
        }
        StringBuffer stringBuffer = new StringBuffer();
        while (!stack2.empty()){
            stringBuffer.append(stack2.pop());
        }
        return stringBuffer.toString();
    }
}
